package com.zj.osm.view.mine.changedb

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.util.TypedValue
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.zj.osm.R
import com.zj.osm.utils.ProgressDialogUtil
import com.zj.osm.utils.ThreadManager
import com.zj.osm.view.BaseActivity
import com.zj.utils.utils.utilcode.FileUtil
import kotlinx.android.synthetic.main.activity_change_db.*
import java.io.File
import java.util.*


class ChangeDBActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_change_db)
        initView()
    }

    override fun onResume() {
        super.onResume()
        ProgressDialogUtil.getInstence(this)?.progressDialogShow("正在查找相关的数据库...")
        val threadPool = ThreadManager.getThreadPool()
        threadPool.execute {
            lstFile.clear()
            lstFile = FileUtil().getDatabaseFile()
            val msg = mHandler.obtainMessage()
            msg.what = 1
            mHandler.sendMessage(msg)
        }
    }

    private var lstFile = ArrayList<File>()//结果 List
    private val mHandler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message) {
            if (msg.what == 1) {
                //若工作完成，取消动画，初始化界面
                ProgressDialogUtil.getInstence(this@ChangeDBActivity)?.progressDialogDismiss()
                val llm = LinearLayoutManager(this@ChangeDBActivity)
                llm.orientation = LinearLayoutManager.VERTICAL
                dbRecyclerView.layoutManager = llm

                dbRecyclerView.adapter = ChangeDBAdapter(this@ChangeDBActivity,R.layout.item_file_item,lstFile)

            }
        }
    }

    private fun initView() {
        dbTitleBar.setTitle("数据库切换")
        dbTitleBar.setBackImageVisiable(true)

        //获取到TextView的控件
        val textView = dbSearchView.findViewById(R.id.search_src_text) as TextView
        //设置字体大小为14sp
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)//14sp

        dbSearchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            //输入完成后，提交时触发的方法，一般情况是点击输入法中的搜索按钮才会触发，表示现在正式提交了
            override fun onQueryTextSubmit(query: String): Boolean {
                if (TextUtils.isEmpty(query) || query == "") {
                    ToastUtils.showShort("请输入查找内容")

                } else {
                    //ToastUtils.showShort(query)
                    KeyboardUtils.hideSoftInput(this@ChangeDBActivity) //关闭软键盘
                    ProgressDialogUtil.getInstence(this@ChangeDBActivity)?.progressDialogShow("全文检索中,速度较慢，请耐心等待...")
                    val threadPool = ThreadManager.getThreadPool()
                    threadPool.execute {
                        lstFile.clear()
                        lstFile = FileUtil().queryDatabaseFile(query)
                        val msg = mHandler.obtainMessage()
                        msg.what = 1
                        mHandler.sendMessage(msg)
                    }

                }
                return true
            }

            //在输入时触发的方法，当字符真正显示到searchView中才触发，像是拼音，在输入法组词的时候不会触发
            override fun onQueryTextChange(newText: String): Boolean {
                if (TextUtils.isEmpty(newText) || newText == "") {
                    KeyboardUtils.hideSoftInput(this@ChangeDBActivity) //关闭软键盘
                    onResume()
                }
                return true
            }
        })


    }

}
